package com.person.leetcode.editor.cn;

import java.util.Arrays;

/**
 * 64.最小路径和
 * 2022-11-14 11:18:28
 **/
public class Sequence_0064_MinimumPathSum {
    public static void main(String[] args) {
        Solution solution = new Sequence_0064_MinimumPathSum().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int minPathSum(int[][] grid) {
            if (grid.length == 0 || grid[0].length == 0) {
                return 0;
            }
            int row = grid.length;
            int col = grid[0].length;
            int[][] ans = new int[row][col];
            ans[0][0] = grid[0][0];
            //第一行
            for (int i = 1; i < col; i++) {
                ans[0][i] = grid[0][i] + ans[0][i - 1];
            }
            //第一列
            for (int i = 1; i < row; i++) {
                ans[i][0] = grid[i][0] + ans[i - 1][0];
            }
            for (int i = 1; i < row; i++) {
                for (int j = 1; j < col; j++) {
                    ans[i][j] = Math.min(ans[i - 1][j], ans[i][j - 1]) + grid[i][j];
                }
            }

            return ans[row - 1][col - 1];
        }
    }

//leetcode submit region end(Prohibit modification and deletion)

}